<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${package}.dao.${className}Dao">

    <select id="get" resultType="${package}.domain.${className}DO">
        select <#list columns as column>  `${column.columnName}` </#list> from ${tableName} where ${pk.columnName} = ${r"#{value}"}
    </select>

    <select id="list" resultType="${package}.domain.${className}DO">
        select <#list columns as column> `${column.columnName}`  </#list> from ${tableName}
        <where>
           <#list columns as column>
            <if test=" ${column.attrname} != null and  ${column.attrname}.trim() != ''"> and ${column.columnName} = <#noparse>#</#noparse> {${column.attrname}} </if>
           </#list>
        </where>
        <choose>
            <when test="sort != null and sort.trim() != ''">
                order by <#noparse>$</#noparse> {sort} <#noparse>$</#noparse> {order}
            </when>
            <otherwise>
                order by ${pk.columnName} desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit <#noparse>#</#noparse> {offset}, <#noparse>#</#noparse> {limit}
        </if>
    </select>

    <select id="count" resultType="int">
        select count(*) from ${tableName}
        <where>
           <#list columns as column>
            <if test=" ${column.attrname} != null and ${column.attrname}.trim() != ''"> and ${column.columnName} = <#noparse>#</#noparse> ${column.attrname} </if>
           </#list>
        </where>
    </select>

    <insert id="save" parameterType="${package}.domain.${className}DO"  useGeneratedKeys="true" keyProperty="$pk.attrname" >
    insert into ${tableName}
    (
    <#list columns as column>
          `${column.columnName}`
    </#list>
    )
    values
    (
   <#list columns as column>
           <#noparse>#</#noparse> ${column.attrname}
   </#list>
    )
    </insert>

    <update id="update" parameterType="${package}.domain.${className}DO">
        update ${tableName}
        <set>
           <#list columns as column>
            <#if   column.columnName != '${pk.columnName}' >
            <if test=" ${column.attrname} != null">`${column.columnName}` = <#noparse>#</#noparse> {${column.attrname}}</if>
            </#if>
           </#list>
        </set>
        where ${pk.columnName} = <#noparse>#</#noparse>{${pk.attrname}}
    </update>

    <delete id="remove">
        delete from ${tableName} where ${pk.columnName} = ${r"#{value}"}
    </delete>

    <delete id="batchRemove">
        delete from ${tableName} where ${pk.columnName} in
        <foreach item="${pk.attrname}" collection="array" open="(" separator="," close=")">
       <#noparse>#</#noparse> {${pk.attrname}}
        </foreach>
    </delete>
</mapper>